/**
 * Created by Administrator on 2017/3/1.
 */
import {allUrl} from '../scripts/url'
import {Toast,checkLogin,getFormatDateByLong} from '../scripts/tip'

export const  orderDetails =(orderId,orderNumber) => {

    $.getJSON(''+allUrl()+'mall/queryOrderDetail?orderId='+orderId+'&orderNumber='+orderNumber+'',  (data) => {
        var order,product;
        order=data.body.order;
        product=data.body.product;
        showDetials(order,product)
    })
};

function showDetials(order,product){
    var orderId,orderNumber,userId,totalPrice,actualPrice,freight,rewardMoney,voucherMoney,
        status,payAmount,sign,createTime,realName,telephone,province,city,dist,address,idNumber,
        gender,height,wechatId,productId,productName,productType,productAttribute,productDetailId,color,size,count,productPrice,productUrl;
    var statusText='';

    orderId=order.orderId;
    orderNumber=order.orderNumber;
    userId=order.userId;
    totalPrice=order.totalPrice;
    actualPrice=order.actualPrice;
    freight=order.freight;
    rewardMoney=order.rewardMoney;
    voucherMoney=order.voucherMoney;
    status=order.status;
    payAmount=order.payAmount;
    sign=order.sign;
    createTime=order.createTime;
    realName=order.realName;
    telephone=order.telephone;
    province=order.province;
    city=order.city;
    dist=order.dist||'';
    address=order.address;
    idNumber=order.idNumber;
    gender=order.gender;
    height=order.height;
    wechatId=order.wechatId;

    productId=product.productId;
    productName=product.productName;
    productType=product.productType;
    productAttribute=product.productAttribute;
    productDetailId=product.category.productDetailId;
    color=product.category.color;
    size=product.category.size;
    count=product.category.count;
    productPrice=product.category.productPrice;
    productUrl=product.category.productUrl;


    if(status===0){
        statusText='待付款';
        $('.BuyBtn').addClass('hide');
        $('.OrderDetailsPayBtn').removeClass('hide');
    }else {
        $('.OrderDetailsPayBtn ').addClass('hide');

        if(status===4){
            statusText='已取消';
            $('.BuyBtn').removeClass('hide');
            $('.BuyBtn a').html('重新购买').addClass('aginBuy').attr('href','#/mall/productDetails?productId='+productId+'');
        }else {
            $('.BuyBtn').addClass('hide');
            if(status===1){
                statusText='待发货';
            }else if(status===2){
                statusText='已发货';
                $('.BuyBtn a').html('已发货');
            }else if(status===3){
                statusText='已完成';
                $('.BuyBtn a').html('已完成');
            }else if(status===5){
                statusText='已关闭';
                $('.BuyBtn a').html('关闭');
            }else if(status==6){
                statusText='待审核';
                $('.BuyBtn a').html('待审核');
            } else {
                statusText='已退款';
                $('.BuyBtn a').html('已退款');
            }
        }
    }

    $('.OrderDetailsStatus .status').html('订单状态：'+statusText+'');
    $('.OrderDetailsStatus .orderN').html('订单号：'+orderNumber+'');

    $('.OrderDetailsStatus .orderP').html('订单金额：￥'+totalPrice+'');
    $('.OrderDetailsStatus .orderCtime').html('下单时间：'+getFormatDateByLong(createTime,'yyyy-MM-dd hh:mm:ss')+'');

    $('.ExistingAddr .name').html('收货人：'+realName+'');
    $('.ExistingAddr .phone').html(telephone);


    $('.ExistingAddr  .detailsAddr').html('收货地址：'+address+'');

    if(productType===1) $('.ExistingAddr .detailsAddr').html('身份证号：'+idNumber+'');

    $('.OrderDetails .liWrap .product-img ').css({background:'url("'+productUrl+'")',backgroundSize:'100% 100%'});
    $('.OrderDetails .liWrap .order-name h1 ').html(productName);
    $('.OrderDetails .liWrap .order-name h2 ').html(color+' '+size);
    $('.OrderDetails .liWrap .order-price h1 ').html('￥'+productPrice+'');
    $('.OrderDetails .liWrap .order-price h2 ').html('x'+count+'');

    $('.OrderDetails .payDetails .productPrice ').html('￥'+productPrice+'');
    $('.OrderDetails .payDetails .productReward ').html('-￥'+rewardMoney+'');
    $('.OrderDetails .payDetails .productVoucherMoney ').html('-￥'+voucherMoney+'');
    $('.OrderDetails .payDetails .productFteight ').html('-￥'+freight+'');
    $('.OrderDetails .payDetails .productRealPice ').html('-￥'+actualPrice+'');


    //TODO 取消订单
    $('.cancelOrder').click(function(){

        layer.open({
            content: '<div class="cancelTip">确定取消吗？</div>',
            style: 'position:fixed; top:35%; width:80%; left:50%;margin-left:-40%;border-radius: 8px;background:#fff',
            btn: ['确定', '取消'],
            yes: function(index){
                cancelOrder(orderId);
                location.reload();
                layer.close(index);
            }
        });
    });

    //TODO 确认buy
    $('.confirmBuy').click(function(){
        asyncPay(sign,productName,payAmount,orderNumber)
    })

}

function cancelOrder(orderId){

    $.post(''+allUrl()+'mall/cancelCustomerOrder?orderId='+orderId+'&reason=',  (data) => {

        data=$.parseJSON(data);

        checkLogin(data, false, '');

        if(data.head.returnCode===0){

            if(data.head.subCode===0){

            }else {
                Toast(data.head.subMsg,2000,'');
            }
        }else {
            Toast(data.head.returnMsg,2000,'');
        }
    })
}


function bcPay(sign,productName,payAmount,orderNumber) {

    BC.err = function(data) {
        //注册错误信息接受
        alert(data["ERROR"]);
    };


    //3. 调用BC.click 接口传递参数

    BC.click({
        "title":productName, //商品名
        "amount":payAmount,  //总价（分）
        "out_trade_no":orderNumber, //自定义订单号
        "instant_channel":"ali",//设置后直接调用支付
        "use_app":false,
        "sign":sign, //商品信息hash值，含义和生成方式见下文
        "return_url" : "https://dev.api.leqibike.com/payHook" //支付成功后跳转的商户页面,可选，默认为http://payservice.beecloud.cn/spay/result.php
    });
}

function asyncPay(sign,productName,payAmount,orderNumber) {
    if (typeof BC == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('beecloud:onready', bcPay, false);
        }else if (document.attachEvent){
            document.attachEvent('beecloud:onready', bcPay);
        }
    }else{
        bcPay(sign,productName,payAmount,orderNumber);
    }
}